<?php

/**
 *
 * @package conf
 * @author jianqing
 */
require_once "../global.php";

// referer
$referer = $_SERVER['HTTP_REFERER'];

// 对应特殊网站特殊处理js配置文件
$clientSiteConf = STATIC_ROOT_DIR.'data/clientsitejs.json';

// jipin基础js地址
$jipinClientPath = STATIC_ROOT_DIR.'htdocs/js/client/jipin/';
$jsPath = $jipinClientPath.'jipin.js';

// 特殊网站单独js地址
$extraJs = getSiteJs($referer, $clientSiteConf);
if(!is_null($extraJs)) 
{
	$extraJsPath = $jipinClientPath.$extraJs;
}

// 替换全局变量，输出jipin js
if (is_file($jsPath))
{
	// 获取js输出内容
    $content = file_get_contents($jsPath);
	if(is_file($extraJsPath)) 
	{
		$content .= file_get_contents($extraJsPath);
	}
	
	// 模板变量替换
	$content = replaceVars($content);
	
	// 输出
    header("Content-Type: application/javascript");
    header("Cache-Control: no-cache");
    echo $content;
    exit;
}
else
{
    header("HTTP/1.0 404 Not Found");
}

/**
 * 模板变量替换
 */
function replaceVars($content)
{
    $content = str_replace('{_IMG_HOST_}', IMG_HOST, $content);
    $content = str_replace('{_STATIC_HOST_}', STATIC_HOST, $content);
    $content = str_replace('{_LOG_HOST_}', LOG_HOST, $content);
    $content = str_replace('{_APITU_HOST_}', APITU_HOST, $content);

    $content = str_replace('{_PIC_NORMAL_WWW_HOST_}', PIC_NORMAL_WWW_HOST, $content);
	$content = str_replace('{_PIC_LITE_WWW_HOST_}', PIC_LITE_WWW_HOST, $content);
    return $content;
}

/**
 * 特殊网站js获取方法
 */
function getSiteJs($url, $clientSiteConf) 
{
	$extraJS = NULL;
	if(is_file($clientSiteConf)) 
	{
		$conf = json_decode(file_get_contents($clientSiteConf));
		foreach ($conf as $site => $js) 
		{
			if(strpos($url, $site) !== false) 
			{
				$extraJS = $js;
				break;
			}
		}
	}
	return $extraJS;
}

exit;
?>